package com.bricker.laboratorymanagement.business.controller;

import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bricker.laboratorymanagement.business.entity.Approval;
import com.bricker.laboratorymanagement.business.model.AppSelectModel;
import com.bricker.laboratorymanagement.business.service.ApprovalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/approval")
public class ApprovalController {
    @Autowired
    private ApprovalService approvalService;

    @PostMapping("/joinGroup")
    public Map<String,String> joinGroup(HttpServletRequest request, @RequestParam("groupId") Long groupId){
        String token=request.getHeader("access_token");
        Long uId= Long.valueOf(JWT.decode(token).getAudience().get(0));
        return approvalService.joinGroup(uId,groupId);
    }

    @PostMapping("/reserveLab")
    public Map<String, String> reserveLab(HttpServletRequest request,@RequestBody Approval approval) {
        String token=request.getHeader("access_token");
        Long uId= Long.valueOf(JWT.decode(token).getAudience().get(0));
        return approvalService.reserveLab(uId, approval);
    }

    @GetMapping
    public Page<Approval> getApproval(HttpServletRequest request,AppSelectModel appSelectModel){
        String token=request.getHeader("access_token");
        Long uId= Long.valueOf(JWT.decode(token).getAudience().get(0));
        appSelectModel.setUId(uId);
        return approvalService.getApproval(appSelectModel);
    }

    @PostMapping("/updateStatus")
    public Map<String, String> updateStatus(HttpServletRequest request,@RequestParam("state")String state,@RequestParam("id") Long id){
        String token=request.getHeader("access_token");
        Long uId= Long.valueOf(JWT.decode(token).getAudience().get(0));
        return approvalService.updateStatus(uId,state,id);
    }

    @PostMapping("/approvalReseve")
    public Map<String, String> approvalReseve(HttpServletRequest request,@RequestParam("state")String state,@RequestParam("id") Long id) {
        String token=request.getHeader("access_token");
        Long uId= Long.valueOf(JWT.decode(token).getAudience().get(0));
        return approvalService.approvalReseve(uId, state, id);
    }


}
